// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.savefile;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import zombie.core.BoxedStaticValues;
import zombie.core.Core;
import zombie.core.logger.ExceptionLogger;
import zombie.debug.DebugLog;
import zombie.util.PZSQLUtils;
import zombie.vehicles.VehicleDBHelper;

public final class PlayerDBHelper {
    public static Connection create() {
        Connection connection = null;
        String string = zombie.ZomboidFileSystem.instance.getCurrentSaveDir();
        File file0 = new File(string);
        if (!file0.exists()) {
            file0.mkdirs();
        }

        File file1 = new File(string + File.separator + "players.db");
        file1.setReadable(true, false);
        file1.setExecutable(true, false);
        file1.setWritable(true, false);
        if (!file1.exists()) {
            try {
                file1.createNewFile();
                connection = PZSQLUtils.getConnection(file1.getAbsolutePath());
                Statement statement0 = connection.createStatement();
                statement0.executeUpdate(
                    "CREATE TABLE localPlayers (id   INTEGER PRIMARY KEY NOT NULL,name STRING,wx    INTEGER,wy    INTEGER,x    FLOAT,y    FLOAT,z    FLOAT,worldversion    INTEGER,data BLOB,isDead BOOLEAN);"
                );
                statement0.executeUpdate(
                    "CREATE TABLE networkPlayers (id   INTEGER PRIMARY KEY NOT NULL,world TEXT,username TEXT,playerIndex   INTEGER,name STRING,steamid STRING,x    FLOAT,y    FLOAT,z    FLOAT,worldversion    INTEGER,data BLOB,isDead BOOLEAN);"
                );
                statement0.executeUpdate("CREATE INDEX inpusername ON networkPlayers (username);");
                statement0.close();
            } catch (Exception exception0) {
                ExceptionLogger.logException(exception0);
                DebugLog.log("failed to create players database");
                System.exit(1);
            }
        }

        if (connection == null) {
            try {
                connection = PZSQLUtils.getConnection(file1.getAbsolutePath());
            } catch (Exception exception1) {
                ExceptionLogger.logException(exception1);
                DebugLog.log("failed to create players database");
                System.exit(1);
            }
        }

        try {
            Statement statement1 = connection.createStatement();
            statement1.executeQuery("PRAGMA JOURNAL_MODE=TRUNCATE;");
            statement1.close();
        } catch (Exception exception2) {
            ExceptionLogger.logException(exception2);
            DebugLog.log("failed to config players.db");
            System.exit(1);
        }

        try {
            connection.setAutoCommit(false);
        } catch (SQLException sQLException) {
            DebugLog.log("failed to setAutoCommit for players.db");
        }

        return connection;
    }

    public static void rollback(Connection conn) {
        if (conn != null) {
            try {
                conn.rollback();
            } catch (SQLException sQLException) {
                ExceptionLogger.logException(sQLException);
            }
        }
    }

    public static boolean isPlayerAlive(String saveDir, int playerSqlId) {
        if (Core.getInstance().isNoSave()) {
            return false;
        } else {
            File file0 = new File(saveDir + File.separator + "map_p.bin");
            if (file0.exists()) {
                return true;
            } else if (VehicleDBHelper.isPlayerAlive(saveDir, playerSqlId)) {
                return true;
            } else if (playerSqlId == -1) {
                return false;
            } else {
                try {
                    File file1 = new File(saveDir + File.separator + "players.db");
                    if (!file1.exists()) {
                        return false;
                    } else {
                        file1.setReadable(true, false);
                        Connection connection = PZSQLUtils.getConnection(file1.getAbsolutePath());

                        boolean _boolean;
                        label117: {
                            try {
                                String string = "SELECT isDead FROM localPlayers WHERE id=?";
                                PreparedStatement preparedStatement = connection.prepareStatement(string);

                                label105: {
                                    try {
                                        preparedStatement.setInt(1, playerSqlId);
                                        ResultSet resultSet = preparedStatement.executeQuery();
                                        if (!resultSet.next()) {
                                            break label105;
                                        }

                                        _boolean = !resultSet.getBoolean(1);
                                    } catch (Throwable throwable0) {
                                        if (preparedStatement != null) {
                                            try {
                                                preparedStatement.close();
                                            } catch (Throwable throwable1) {
                                                throwable0.addSuppressed(throwable1);
                                            }
                                        }

                                        throw throwable0;
                                    }

                                    if (preparedStatement != null) {
                                        preparedStatement.close();
                                    }
                                    break label117;
                                }

                                if (preparedStatement != null) {
                                    preparedStatement.close();
                                }
                            } catch (Throwable throwable2) {
                                if (connection != null) {
                                    try {
                                        connection.close();
                                    } catch (Throwable throwable3) {
                                        throwable2.addSuppressed(throwable3);
                                    }
                                }

                                throw throwable2;
                            }

                            if (connection != null) {
                                connection.close();
                            }

                            return false;
                        }

                        if (connection != null) {
                            connection.close();
                        }

                        return _boolean;
                    }
                } catch (Throwable throwable4) {
                    ExceptionLogger.logException(throwable4);
                    return false;
                }
            }
        }
    }

    public static ArrayList<Object> getPlayers(String saveDir) throws SQLException {
        ArrayList arrayList = new ArrayList();
        if (Core.getInstance().isNoSave()) {
            return arrayList;
        } else {
            File file = new File(saveDir + File.separator + "players.db");
            if (!file.exists()) {
                return arrayList;
            } else {
                file.setReadable(true, false);
                Connection connection = PZSQLUtils.getConnection(file.getAbsolutePath());

                try {
                    String string0 = "SELECT id, name, isDead FROM localPlayers";
                    PreparedStatement preparedStatement = connection.prepareStatement(string0);

                    try {
                        ResultSet resultSet = preparedStatement.executeQuery();

                        while (resultSet.next()) {
                            int _int = resultSet.getInt(1);
                            String string1 = resultSet.getString(2);
                            boolean _boolean = resultSet.getBoolean(3);
                            arrayList.add(BoxedStaticValues.toDouble((double)_int));
                            arrayList.add(string1);
                            arrayList.add(_boolean ? Boolean.TRUE : Boolean.FALSE);
                        }
                    } catch (Throwable throwable0) {
                        if (preparedStatement != null) {
                            try {
                                preparedStatement.close();
                            } catch (Throwable throwable1) {
                                throwable0.addSuppressed(throwable1);
                            }
                        }

                        throw throwable0;
                    }

                    if (preparedStatement != null) {
                        preparedStatement.close();
                    }
                } catch (Throwable throwable2) {
                    if (connection != null) {
                        try {
                            connection.close();
                        } catch (Throwable throwable3) {
                            throwable2.addSuppressed(throwable3);
                        }
                    }

                    throw throwable2;
                }

                if (connection != null) {
                    connection.close();
                }

                return arrayList;
            }
        }
    }

    public static void setPlayer1(String saveDir, int sqlID) throws SQLException {
        if (!Core.getInstance().isNoSave()) {
            if (sqlID != 1) {
                File file = new File(saveDir + File.separator + "players.db");
                if (file.exists()) {
                    file.setReadable(true, false);
                    Connection connection = PZSQLUtils.getConnection(file.getAbsolutePath());

                    label181: {
                        label182: {
                            label183: {
                                try {
                                    boolean boolean0 = false;
                                    boolean boolean1 = false;
                                    int int0 = -1;
                                    int int1 = -1;
                                    String string = "SELECT id FROM localPlayers";
                                    PreparedStatement preparedStatement = connection.prepareStatement(string);

                                    try {
                                        ResultSet resultSet = preparedStatement.executeQuery();

                                        while (resultSet.next()) {
                                            int int2 = resultSet.getInt(1);
                                            if (int2 == 1) {
                                                boolean0 = true;
                                            } else if (int0 == -1 || int0 > int2) {
                                                int0 = int2;
                                            }

                                            if (int2 == sqlID) {
                                                boolean1 = true;
                                            }

                                            int1 = Math.max(int1, int2);
                                        }
                                    } catch (Throwable throwable0) {
                                        if (preparedStatement != null) {
                                            try {
                                                preparedStatement.close();
                                            } catch (Throwable throwable1) {
                                                throwable0.addSuppressed(throwable1);
                                            }
                                        }

                                        throw throwable0;
                                    }

                                    if (preparedStatement != null) {
                                        preparedStatement.close();
                                    }

                                    if (sqlID <= 0) {
                                        if (!boolean0) {
                                            break label181;
                                        }

                                        string = "UPDATE localPlayers SET id=? WHERE id=?";
                                        preparedStatement = connection.prepareStatement(string);

                                        try {
                                            preparedStatement.setInt(1, int1 + 1);
                                            preparedStatement.setInt(2, 1);
                                            preparedStatement.executeUpdate();
                                        } catch (Throwable throwable2) {
                                            if (preparedStatement != null) {
                                                try {
                                                    preparedStatement.close();
                                                } catch (Throwable throwable3) {
                                                    throwable2.addSuppressed(throwable3);
                                                }
                                            }

                                            throw throwable2;
                                        }

                                        if (preparedStatement != null) {
                                            preparedStatement.close();
                                        }
                                        break label182;
                                    }

                                    if (!boolean1) {
                                        break label183;
                                    }

                                    if (boolean0) {
                                        string = "UPDATE localPlayers SET id=? WHERE id=?";
                                        preparedStatement = connection.prepareStatement(string);

                                        try {
                                            preparedStatement.setInt(1, int1 + 1);
                                            preparedStatement.setInt(2, 1);
                                            preparedStatement.executeUpdate();
                                            preparedStatement.setInt(1, 1);
                                            preparedStatement.setInt(2, sqlID);
                                            preparedStatement.executeUpdate();
                                            preparedStatement.setInt(1, sqlID);
                                            preparedStatement.setInt(2, int1 + 1);
                                            preparedStatement.executeUpdate();
                                        } catch (Throwable throwable4) {
                                            if (preparedStatement != null) {
                                                try {
                                                    preparedStatement.close();
                                                } catch (Throwable throwable5) {
                                                    throwable4.addSuppressed(throwable5);
                                                }
                                            }

                                            throw throwable4;
                                        }

                                        if (preparedStatement != null) {
                                            preparedStatement.close();
                                        }
                                    } else {
                                        string = "UPDATE localPlayers SET id=? WHERE id=?";
                                        preparedStatement = connection.prepareStatement(string);

                                        try {
                                            preparedStatement.setInt(1, 1);
                                            preparedStatement.setInt(2, sqlID);
                                            preparedStatement.executeUpdate();
                                        } catch (Throwable throwable6) {
                                            if (preparedStatement != null) {
                                                try {
                                                    preparedStatement.close();
                                                } catch (Throwable throwable7) {
                                                    throwable6.addSuppressed(throwable7);
                                                }
                                            }

                                            throw throwable6;
                                        }

                                        if (preparedStatement != null) {
                                            preparedStatement.close();
                                        }
                                    }
                                } catch (Throwable throwable8) {
                                    if (connection != null) {
                                        try {
                                            connection.close();
                                        } catch (Throwable throwable9) {
                                            throwable8.addSuppressed(throwable9);
                                        }
                                    }

                                    throw throwable8;
                                }

                                if (connection != null) {
                                    connection.close();
                                }

                                return;
                            }

                            if (connection != null) {
                                connection.close();
                            }

                            return;
                        }

                        if (connection != null) {
                            connection.close();
                        }

                        return;
                    }

                    if (connection != null) {
                        connection.close();
                    }
                }
            }
        }
    }
}
